<template>
  <div class="wrap">
    <el-row>
      <!--工具条-->
      <el-col :span="24" class="header-wrap">
        <el-form :inline="true" :model="formInline" :rules="rules" ref="formInline">
          <el-form-item label="任务名" prop="jobName">
            <el-input v-model="formInline.jobName" prefix-icon="el-icon-search"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="add">新增</el-button>
            <el-button type="primary" @click="query('formInline')">查询</el-button>
            <el-button type="danger" @click="reset('formInline')">重置</el-button>
            <el-button type="primary" @click="handBuildOverdue">手动逾期</el-button>
            <el-button type="primary" @click="handWithhold">手动代扣</el-button>
          </el-form-item>
        </el-form>
      </el-col>
      <!--表格-->
      <el-col :span="24">
        <el-table :data="taskList" :border="true" style="width: 100%;">
          <el-table-column label="任务名" porp="jobName" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.jobName }}</span>
            </template>
          </el-table-column>
          <el-table-column label="任务组" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.jobGroup }}</span>
            </template>
          </el-table-column>
          <el-table-column label="任务描述" align="center" min-width="135px">
            <template slot-scope="scope">
              <span>{{ scope.row.description }}</span>
            </template>
          </el-table-column>
          <el-table-column label="任务方法名" align="center">
            <template slot-scope="scope">
              <span>{{ scope.row.methodName }}</span>
            </template>
          </el-table-column>
          <el-table-column label="创建时间" align="center" prop="createTimeStr">

          </el-table-column>
          <el-table-column label="上次执行时间" align="center" prop="previousFireTime">

          </el-table-column>
          <el-table-column label="状态" align="center">
            <template slot-scope="scope">
              <el-tag type="success" v-if="scope.row.status == 'NORMAL'">启用</el-tag>
              <el-tag type="danger" v-if="scope.row.status == 'PAUSED'">禁用</el-tag>
            </template>
          </el-table-column>
          <el-table-column prop="status" fixed="right" label="操作" :span="4" min-width="140px" align="center">
            <template slot-scope="scope">
              <el-button @click="handleEdit(scope.$index, scope.row)" size="mini" type="primary">编辑</el-button>
              <el-button v-if="scope.row.status == 'PAUSED'" type="success" size="mini" @click="handleEnable(scope.$index, scope.row)">启用</el-button>
              <el-button v-if="scope.row.status == 'NORMAL'" type="danger" size="mini" @click="handleDisable(scope.$index, scope.row)">禁用</el-button>
              <el-button type="primary" size="mini" @click="oneTimeRun(scope.$index, scope.row)">立即执行一次</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          class="top"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-sizes="[10, 15, 20, 30]"
          :page-size="size"
          layout="total, sizes, prev, pager, next, jumper"
          :total='total'>
        </el-pagination>
      </el-col>
    </el-row>
    <!--新增任务-->
    <el-dialog title="新增任务" :visible.sync="dialogFormVisible" width="40%" :close-on-click-modal="false">
      <el-form :model="form" ref="form" :rules="rules">
        <el-form-item label="任务名"  prop="name">
          <el-input v-model="form.jobName"></el-input>
        </el-form-item>
        <el-form-item label="任务组"  prop="jobGroup">
          <el-input v-model="form.jobGroup"></el-input>
        </el-form-item>
        <el-form-item label="任务描述"  prop="description">
          <el-input v-model="form.description"></el-input>
        </el-form-item>
        <el-form-item label="任务方法名" prop="methodName">
          <el-input v-model="form.methodName"></el-input>
        </el-form-item>
        <el-form-item label="任务类名" prop="className">
          <el-input v-model="form.className"></el-input>
        </el-form-item>
        <el-form-item label="任务表达式" prop="cronExpression">
          <el-input v-model="form.cronExpression"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submit1('form')">提交</el-button>
      </div>
    </el-dialog>
    <!--编辑界面-->
    <el-dialog title="编辑" :visible.sync="editFormVisible" width="40%" :close-on-click-modal="false">
      <el-form :model="editForm" ref="editForm" :rules="rules">
        <el-form-item label="任务名"  prop="jobName">
          <el-input v-model="editForm.jobName"></el-input>
        </el-form-item>
        <el-form-item label="任务组"  prop="jobGroup">
          <el-input v-model="editForm.jobGroup"></el-input>
        </el-form-item>
        <el-form-item label="表达式" prop="cronExpression">
          <el-input v-model="editForm.cronExpression"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="editFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="submit('editForm')">提交</el-button>
      </div>
    </el-dialog>

  </div>

</template>

<script type="text/ecmascript-6">
  import {post} from '@/utils/http'
  export default {
    data() {
      let validatejobName = (rule, value, callback) => {
        if (value === '') {
          callback();
        } else {
          callback();
        }
      };
      return {
        // 个人信息存值
        dialogFormVisible: false,
        taskList: [],
        currentPage: 1,
        total: 0,
        size: 10,
        formInline: {
          jobName: '',
        },
        //新增界面数据
        form: {
          'jobName':'',
          'jobGroup':'',
          'description':'',
          'methodName':'',
          'className': '',
          'cronExpression': ''
        },
        editFormVisible: false,//编辑界面是否显示
        editLoading: false,
        //编辑页面数据
        editForm:{
          'jobName':'',
          'jobGroup':'',
          'cronExpression':''
        },
        idd: '',
        rules: {
          jobName: [
            { validator: validatejobName, trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      // 格式化时间
      dateFormat(row,column) {
        let tableTime = row[column.property];
        if (tableTime == undefined) {
          return "";
        }
        return this.moment(tableTime).utc().format("YYYY-MM-DD HH:mm:ss");
      },
      getList() {
        post('/api/quartz/v1/job/find',{pageIndex: this.currentPage,pageSize: this.size}).then((response) => {
          this.taskList = response.data;
          this.total = response.data.totalCount;
        })
      },
      reset(formInline) {
        this.$refs[formInline].resetFields();
      },
      query(formInline) {
        this.$refs[formInline].validate((vaild) => {
          if(vaild) {
            // 发送请求查询
            let para = {
              pageSize: this.size,
              pageIndex: this.currentPage
            }
            if(!this.formInline.jobName.replace(/\s/g, "") == '') {
              para.jobName = this.formInline.jobName.replace(/\s/g, "")
            }
            post('/api/quartz/v1/job/find',para).then((response) => {
              this.taskList = response.data;
              this.total = response.data.totalCount;
              this.size = response.data.pageSize;
            })
          }
        })
      },

      //手动逾期
      handBuildOverdue() {
        post('/api/manager/v1/loan/handBuildOverdue',{}).then((response) => {
          this.$message({
            showClose: true,
            message: '手动逾期成功',
            type: 'success'
          });
        })
      },
      // 手动代扣
      handWithhold() {
        post('/api/manager/v1/loan/handWithhold',{}).then((response) => {
          this.$message({
            showClose: true,
            message: '手动代扣成功',
            type: 'success'
          });
        })
      },
      //运行一次
      oneTimeRun : function (index, row) {
        this.$confirm('确认执行吗?', '提示', {
          type: 'warning'
        }).then(() => {
          this.listLoading = true;
          post('/api/quartz/v1/job/startNow',{id: row.id,jobName: row.jobName, jobGroup: row.jobGroup}).then((response) => {
            this.listLoading = false;
            this.$message({
              message: '执行成功',
              type: 'success'
            });
            this.getList();
          });
        }).catch(function(err) {
          console.log(err)
        });
      },
      // 启用
      handleEnable: function (index, row) {
        this.$confirm('确认启用该用户吗?', '提示', {
          type: 'warning'
        }).then(() => {
          this.listLoading = true;
          post('/api/quartz/v1/job/resume',{id: row.id,jobName: row.jobName, jobGroup: row.jobGroup}).then((response) => {
            this.listLoading = false;
            this.$message({
              message: '启用成功',
              type: 'success'
            });
            this.getList();
          });
        })
      },
      // 禁用
      handleDisable: function (index, row) {
        this.$confirm('确认禁用该用户吗?', '提示', {
          type: 'warning'
        }).then(() => {
          this.listLoading = true;
          post('/api/quartz/v1/job/stopJob',{id: row.id,jobName: row.jobName, jobGroup: row.jobGroup}).then((response) => {
            this.listLoading = false;
            this.$message({
              message: '禁用成功',
              type: 'success'
            });
            this.getList();
          });
        })
      },

      //显示新增界面
      add() {
        this.dialogFormVisible = true;
      },
      // 新增提交
      submit1(form) {
        this.dialogFormVisible = false
        let para = {
          jobName: this.form.jobName,
          jobGroup: this.form.jobGroup,
          className: this.form.className,
          description: this.form.description,
          methodName: this.form.methodName,
          cronExpression: this.form.cronExpression
        };
        post('/api/quartz/v1/job/add',para).then((response) => {
          this.getList()
        });
       /* this.getList();*/
        this.$refs[form].validate((vaild) => {
          if(vaild) {
            // 发送请求查询

          }
        })
      },

      //显示编辑页面
      handleEdit: function (index,row) {
        this.editFormVisible = true;
        this.editForm.jobName = row.jobName;
        this.editForm.jobGroup = row.jobGroup;
        this.editForm.cronExpression = row.cronExpression;
      },
      // 编辑提交
      submit(editForm) {
        this.$refs[editForm].validate((vaild) => {
          if(vaild) {
            let para = {
              pageIndex: this.currentPage,
              pageSize: this.size,
              jobName: this.editForm.jobName,
              jobGroup: this.editForm.jobGroup,
              cronExpression: this.editForm.cronExpression
            }
            post('/api/quartz/v1/job/update',para).then((response) => {
              this.editFormVisible = false
              this.getList();
              /*this.taskList = response.data;
              this.total = response.data.totalCount;*/
            })
          }
        })
      },

      handleSizeChange(val) {
        this.size = val;
        this.getList();
      },
      handleCurrentChange(val) {
        this.currentPage =val;
        this.getList();
      }
    },
    mounted(){
      this.getList();
    },

  }
</script>

<style lang='scss' scoped>
  .wrap {
    .header-wrap{
      margin-bottom: 60px;
      background-color: #66BFBF;
      border-radius: 6px;
    }
    .el-form-item {
      margin-top: 22px;
      margin-left: 22px;
    }
    padding: 20px;
    .pad .el-col {
      margin-top: 5px;
      margin-bottom: 5px;
    }
    .top {
      margin-top: 20px;
      float: right;
    }
  }
</style>
